
//
//  SearchBarVC.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/12.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit


class LDSearchTagsVC: UIViewController {
    
    var models:SearchViewsCellModels? = nil
    var searingModels:SearchViewsCellModels? = nil
    var searchBars:UISearchBar?
    var isSearching = false
    
    var cellTag = 1111
    var searingtableView:UITableView?
    
    var titleArr = [String]()
    var titleDescArr = [[String]]()
    var titleDescArr_ID = [[String]]()
    
    var tapTagLabSure:((id:String,title:String)->Void)?
    
    
    var tableView:LDBaseTableView?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
    
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        setupTableView()
        requestNet()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //
    func setupTableView() {

        tableView = LDBaseTableView.init(frame: view.frame, style: .Plain)
        tableView?.separatorStyle = .SingleLine
        
        tableView?.contentInset = UIEdgeInsetsMake(0, 0, LDFrame.TabbarH + 18, 0)
        tableView?.translatesAutoresizingMaskIntoConstraints = false
        tableView?.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView?.frame = view.frame
        tableView?.delegate = self
        tableView?.dataSource = self
        
        view.addSubview(tableView!)
    }
    
    override func viewWillDisappear(animated: Bool) {
        
        for item in view.subviews {
            item.removeFromSuperview()
        }
    }
    //网络
    func requestNet() {
        LDCheckPublicInfo.shareInstance.checkoutAllJobCategory { [weak self] (data) in
            
            for i in 0..<data.count {
                
                let title = data[i]["level1_name"] as? String ?? ""
                let leve2 = data[i]["level2"] as? [NSDictionary] ?? [NSDictionary]()
                
                let count = leve2.count ?? 0
                
                var descArr = [String]()
                var descIDArr = [String]()
                
                for i in 0..<count {
                    
                    let level2_name = leve2[i]["level2_name"] as? String ?? ""
                    let level2_id = leve2[i]["level2_id"] as? String ?? ""
                    
                    descArr.append(level2_name)
                    descIDArr.append(level2_id)
                    
                }
                
                self?.titleDescArr.append(descArr)
                self?.titleDescArr_ID.append(descIDArr)
                self?.titleArr.append(title)
            }
            
            NSOperationQueue.mainQueue().addOperationWithBlock({
                self?.tableView!.reloadData()
            })
            
        }
    }
    
    /** 点击标签按钮 */
    func getCategoryIDClosure(closure:((id:String,title:String)->Void)) {
        
        tapTagLabSure = closure
    }
    
    override func viewWillAppear(animated: Bool) {
        
        navigationItem.setHidesBackButton(true, animated: false)
    }
    
    func navBarCancel() {
        
        self.navigationController?.popViewControllerAnimated(true)
    }
    
}

extension LDSearchTagsVC:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        
        return 1
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
            return titleArr.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            var cells = tableView.dequeueReusableCellWithIdentifier("cellls") as? SearchViewsCell
            
            if cells == nil {
                
                cells = SearchViewsCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls")
                
            }
            
            cells?.sendLabClosure({ [weak self] (selCell, index) in
                
                let section = selCell.tag - (self?.cellTag ?? 0)
                let titleid = self?.titleDescArr_ID[section][index] ?? "0"
                let title = self?.titleDescArr[section][index] ?? "0"
                
                if self?.tapTagLabSure != nil {
                    
                    self?.tapTagLabSure!(id: titleid,title: title)
                    self?.tableView?.reloadData()
                }
                })
            
            
            cells?.tag = cellTag + Int(indexPath.row)
            cells?.titleTagsArr = titleDescArr[indexPath.row]
            cells?.title = titleArr[indexPath.row]
            
            return cells!
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
            let titlearr = titleDescArr[indexPath.row]
            let hh = SearchViewsCell.getCellHeight(titlearr)
            return hh
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        searchBars?.resignFirstResponder()
    }
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        
        self.navigationController?.view.endEditing(true)
    }
}

